﻿using System;
using System.IO;
using System.Media;
using LeakView.common;

namespace LeakView.service
{

    class SoundManager : IModule, Splashable
    {
        public static readonly SoundManager me = new SoundManager();

        PlayerBoss boss = new PlayerBoss();

        bool alarmConfirmed = false;

        private SoundManager()
        {

        }

        public void OnModuleInitialize()
        {
            boss.Initialize();
        }

        public void OnModuleTerminated()
        {
            boss.Terminate();
        }

        public void confirmAlarm()
        {
            alarmConfirmed = true;
        }

        public void SetAlarmConfirm(bool flag)
        {
            alarmConfirmed = flag;
        }

        public void splash(bool flag)
        {

            if(AlarmModal.me.AlarmForXL.IsSoundAlarm)
            {
                boss.playError();
            }
            else if(AlarmModal.me.AlarmForLX.IsSoundAlarm || AlarmModal.me.AlarmForYC.IsSoundAlarm || AlarmModal.me.AlarmForSC.IsSoundAlarm)
            {
                boss.playAlarm();
            }
            else
            {
                boss.stop();
            }


            /*
            // 播放声音
            if (MainModule.current.Leaking)
            {
                boss.playError();
                alarmConfirmed = false;
            }
            else
            {
                if (!MainModule.pair.Value.Valid ||
                    !MainModule.status.Value.Value.head ||
                    !MainModule.status.Value.Value.tail)
                {
                    if(!alarmConfirmed)
                    {
                        boss.playAlarm();
                    }
                    else
                    {
                        boss.stop();
                    }
                }
                else
                {
                    boss.stop();
                    alarmConfirmed = false;
                }
            }
            */
        }

    }

    class PlayerBoss
    {

        SimplePlayer alarmPlayer = new SimplePlayer();
        SimplePlayer errorPlayer = new SimplePlayer();

        public void Initialize()
        {
            alarmPlayer.Stream = Properties.Resources.alarm;
            errorPlayer.Stream = Properties.Resources.error;
        }

        public void Terminate()
        {
            errorPlayer.Dispose();
            alarmPlayer.Dispose();
        }

        public void playAlarm()
        {
            errorPlayer.stop();
            alarmPlayer.play();
        }

        public void playError()
        {
            alarmPlayer.stop();
            errorPlayer.play();
        }

        public void stop()
        {
            alarmPlayer.stop();
            errorPlayer.stop();
        }

    }



    class SimplePlayer : IDisposable
    {
        SoundPlayer player = new SoundPlayer();

        bool playing = false;

        public Stream Stream
        {
            set { player.Stream = value; player.Load(); }
            get { return player.Stream; }
        }

        public void Dispose()
        {
            stop();
            player.Dispose();
            player = null;
        }

        public void play()
        {
            if (!playing)
            {
                player.PlayLooping();
                playing = true;
            }
        }

        public void stop()
        {
            if (playing)
            {
                player.Stop();
                playing = false;
            }

        }


    }
}
